﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CodeStash.Addin.Core;

namespace CodeStash.Addin_UnitTests
{
    [TestClass]
    public class CredentialManagerTests
    {
        [TestMethod, ExpectedException(typeof(ArgumentNullException))]
        public void Ensure_missing_OpenId_throws_null_exception_on_missing_email_address()
        {
            LoginDetails login = new LoginDetails();
            CredentialManager.Save(login);
        }

        [TestMethod, ExpectedException(typeof(ArgumentException))]
        public void Ensure_missing_OpenId_throws_exception_on_missing_password_and_openid()
        {
            LoginDetails login = new LoginDetails();
            login.EmailAddress = "Pete";
            CredentialManager.Save(login);
        }

        [TestMethod]
        public void Ensure_setting_password_and_email_address_is_ok()
        {
            LoginDetails login = new LoginDetails { EmailAddress = "Pete", Password = "Bert" };
            Assert.AreEqual(login.Password, "Bert");
        }

        [TestMethod]
        public void Ensure_setting_OpenId_and_email_address_is_ok()
        {
            LoginDetails login = new LoginDetails { EmailAddress = "Pete", Token = "Bert" };
            Assert.AreEqual(login.Token, "Bert");
        }

        [TestMethod]
        public void Save_valid_login_details()
        {
            LoginDetails login = new LoginDetails();
            login.EmailAddress = "pete.ohanlon@gmail.com";
            login.Password = "password";
            CredentialManager.Save(login);
        }

        [TestMethod]
        public void Read_valid_login_details()
        {
            LoginDetails login = new LoginDetails();
            login.EmailAddress = "pete.ohanlon@gmail.com";
            login.Password = "password";
            CredentialManager.Save(login);

            LoginDetails details = CredentialManager.Load();

            Assert.AreEqual(details.EmailAddress, login.EmailAddress);
        }
    }
}
